package cn.charmaine.org.service.impl;

import cn.charmaine.basic.base.service.IBaseService;
import cn.charmaine.basic.base.service.impl.BaseServiceImpl;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.Department;
import cn.charmaine.org.mapper.IDepartmentMapper;
import cn.charmaine.org.query.DepartmentQuery;
import cn.charmaine.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private IDepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.insert(department);
        //设置dirPath
        this.update(department);
    }

    /*@Override
    @Transactional
    public void deleteById(Long id) {
        departmentMapper.deleteById(id);
    }
    */
    @Override
    @Transactional
    public void update(Department department) {
        //设置dirPath
        String dirPth = "";
        if(department.getParent() == null){//顶级
            //1.顶级部门：取出自己的id直接拼接即可(开启 :获取自增主键) //新增调用了这个方法
            dirPth = "/"+department.getId();
        }else{
            //2.其他层级部门：找到老爸的dirPath+"/"+自己的id
            Long parentId = department.getParent().getId();
            Department parent = departmentMapper.selectById(parentId);
            dirPth = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPth);//设置路径
        departmentMapper.update(department);
    }

    /*@Override
    public Department queryById(Long id) {
        return departmentMapper.selectById(id);
    }
*/
    /*@Override
    public List<Department> queryAll() {
        return departmentMapper.selectAll();
    }*/

   /* @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        Long count = departmentMapper.count(query);
        List<Department> rows = departmentMapper.rows(query);
        return new PageList<Department>(count,rows);
    }*/

   /* @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }*/

    @Override
    public List<Department> queryTree() {
        //1.查询所有部门
        List<Department> departments = departmentMapper.selectAll();
        //数据准备：把所有部门信息转为map(方便后边用当前部门得到父级部门)
        Map<Long,Department> map = new HashMap<>();
        for (Department dept : departments) {
            map.put(dept.getId(),dept);
        }
        //2.找出顶级部门
        List<Department> firstLevel = new ArrayList<>();
        for (Department dept : departments) {
            if(dept.getParent_id() == null){
                firstLevel.add(dept);
            }else{
                //3.剩下的可能是二级,三级,四级部门...
                //找到当前部门，不自己设置到他的children的属性上
                //用 当前部门的父级id 查找自己的老爸是谁
                Department parent = map.get(dept.getParent_id());
                if(parent != null){
                    parent.getChildren().add(dept);
                }
            }
        }
        return firstLevel;
    }

}
